package org.black.phoneUtility.sms.schedule.app.fragment;

import org.black.phoneUtility.PhoneUtilityConstant;
import org.black.phoneUtility.R;
import org.black.phoneUtility.sms.schedule.provider.SmsScheduleContract;
import org.black.phoneUtility.sms.schedule.widget.SmsScheduleSendingRecipientsHistoryCursorAdapter;
import org.black.phoneUtility.util.admob.AdmobConstant;

import android.app.AlertDialog;
import android.app.Fragment;
import android.app.LoaderManager.LoaderCallbacks;
import android.content.CursorLoader;
import android.content.DialogInterface;
import android.content.Loader;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CursorAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdSize;
import com.google.android.gms.ads.AdView;

public class SmsScheduleSendingRecordRecipientsHistoryListFragment extends
		Fragment implements LoaderCallbacks<Cursor> {
	public static final String TAG = SmsScheduleSendingRecordRecipientsHistoryListFragment.class
			.getName();
	private static final String LOG_TAG = SmsScheduleSendingRecordRecipientsHistoryListFragment.class
			.getName();
	private ListView smsScheduleSendingRecordRecipientList = null;
	private SmsScheduleSendingRecipientsHistoryCursorAdapter smsScheduleSendingRecipientsHistoryCursorAdapter = null;
	private AlertDialog.Builder forAskingDeleteAllRecordsBuilder = null;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setHasOptionsMenu(true);
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		this.smsScheduleSendingRecipientsHistoryCursorAdapter = new SmsScheduleSendingRecipientsHistoryCursorAdapter(
				getActivity(), null,
				CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);
		this.smsScheduleSendingRecordRecipientList
				.setAdapter(smsScheduleSendingRecipientsHistoryCursorAdapter);
		this.smsScheduleSendingRecipientsHistoryCursorAdapter
				.notifyDataSetChanged();
		getActivity()
				.getLoaderManager()
				.initLoader(
						PhoneUtilityConstant.INIT_LOADER_SMS_SCHEDULE_HISTORY_RECORD_ID,
						null, this);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View result = inflater.inflate(R.layout.sms_schedule_history_list,
				container, false);

		this.smsScheduleSendingRecordRecipientList = (ListView) result
				.findViewById(R.id.smsScheduleHistoryListView);
		TextView emptyTextView = (TextView) result
				.findViewById(R.id.smsScheduleHistoryListViewNoDataTextView);

		this.smsScheduleSendingRecordRecipientList.setEmptyView(emptyTextView);
		this.forAskingDeleteAllRecordsBuilder = new AlertDialog.Builder(
				getActivity());
		this.forAskingDeleteAllRecordsBuilder
				.setMessage(R.string.sms_schedule_history_do_you_really_want_to_clear_all_records);
		this.forAskingDeleteAllRecordsBuilder.setPositiveButton(
				android.R.string.ok, new DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {
						getActivity()
								.getContentResolver()
								.delete(SmsScheduleContract.SmsScheduleSendingRecordRecipients.CONTENT_URI,
										null, null);
						Log.i(LOG_TAG, "Delete all histories records");
					}
				}).setNegativeButton(android.R.string.cancel,
				new DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {

					}

				});

		AdView adView = new AdView(getActivity());
		adView.setAdSize(AdSize.SMART_BANNER);
		adView.setAdUnitId(AdmobConstant.UNIT_ID);
		AdRequest adRequest = new AdRequest.Builder().build();
		LinearLayout adViewLayout = (LinearLayout) result
				.findViewById(R.id.adViewLayout);
		adViewLayout.addView(adView);
		adView.loadAd(adRequest);
		return result;
	}

	@Override
	public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
		super.onCreateOptionsMenu(menu, inflater);
		inflater.inflate(
				R.menu.sms_schedule_sending_recipients_history_list_actions,
				menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case android.R.id.home:
			getActivity().onBackPressed();
			return false;
		case R.id.menu_sms_schedule_sending_record_recipients_list_clear_all_log:
			this.forAskingDeleteAllRecordsBuilder.show();
			return true;
		default:
			return super.onOptionsItemSelected(item);
		}
	}

	@Override
	public Loader<Cursor> onCreateLoader(int id, Bundle args) {
		return new CursorLoader(
				getActivity(),
				SmsScheduleContract.SmsScheduleSendingRecordRecipients.CONTENT_URI,
				null,
				null,
				null,
				SmsScheduleContract.SmsScheduleSendingRecordRecipients.CREATION_DATE_TIME
						+ " DESC");
	}

	@Override
	public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
		if (data != null) {
			data.moveToFirst();
			if (smsScheduleSendingRecipientsHistoryCursorAdapter != null) {
				smsScheduleSendingRecipientsHistoryCursorAdapter
						.swapCursor(data);
				smsScheduleSendingRecipientsHistoryCursorAdapter
						.notifyDataSetChanged();
			}
		}
	}

	@Override
	public void onLoaderReset(Loader<Cursor> loader) {
		if (smsScheduleSendingRecipientsHistoryCursorAdapter != null) {
			smsScheduleSendingRecipientsHistoryCursorAdapter.swapCursor(null);
		}
	}

	@Override
	public void onDestroyView() {
		super.onDestroyView();
		smsScheduleSendingRecordRecipientList = null;
		smsScheduleSendingRecipientsHistoryCursorAdapter = null;
		forAskingDeleteAllRecordsBuilder = null;
	}
}
